package com.xrz.controller;

import com.xrz.common.Result;
import com.xrz.dao.QaqUserDao;
import com.xrz.dao.UserInfoDao;
import com.xrz.pojo.QaqUser;
import com.xrz.pojo.UserInfo;
import com.xrz.service.OnlineUserStatsService;
import com.xrz.service.UserService;
import com.xrz.utils.XrzUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Api(tags="登录注册")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    QaqUserDao qaqUserDao;
    @Autowired
    UserService userService;
    @Resource
    UserInfoDao userInfoDao;
    @Resource
    private OnlineUserStatsService onlineUserStatsService;

    //用户名注册
    @ApiOperation(value="用户名注册，作废")
    @PostMapping("/register")
    public Result<?> register(@RequestBody QaqUser user) {
//        userService.register(user);
        QaqUser res = qaqUserDao.selectByUsername(user.getUsername());
        if (res != null) {
            return Result.error(0, "用户名重复");
        }
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        String uid = XrzUtils.getUuid();
        user.setUid(uid);
        UserInfo userInfo=new UserInfo();
        userInfo.setUid(uid);
        userInfoDao.insertSelective(userInfo);
        qaqUserDao.insertSelective(user);
        return Result.success();
    }


    //用户名登录
    @ApiOperation(value = "用户名登录，作废")
    @PostMapping("/login")
    public Result<?> login(@RequestBody QaqUser user) {
        QaqUser qaqUser = qaqUserDao.selectByUsername(user.getUsername());
        if(qaqUser==null){
            return Result.error(0, "用户不存在");
        }
        if(!user.getPassword().equals(qaqUser.getPassword())){
            return Result.error(0, "用户名或密码错误");
        }
        UserInfo userInfo=userInfoDao.selectByUid(qaqUser.getUid());

        Map map=new HashMap<>();
        map.put("qaqUser",qaqUser);
        map.put("userInfo",userInfo);
        //记录用户数
        onlineUserStatsService.online(qaqUser.getId());
        return Result.success(map);
    }

    //手机号，用户名登录
    @ApiOperation(value = "手机号或用户名登录")
    @PostMapping("/loginPhone")
    public Result<?> loginPhone(@RequestBody QaqUser user) {
        return userService.loginPhone(user);
    }

    //注册
    @ApiOperation(value="注册，传用户名，手机号，密码")
    @PostMapping("/registerPhone")
    public Result<?> registerPhone(@RequestBody QaqUser user) {
        return userService.registerPhone(user);
    }

    //获取在线用户
    @ApiOperation(value = "获取在线用户,每分钟请求一次" )
    @GetMapping("/onlineUsers/{userId}")
    public Result<?> onlineUsers(@PathVariable Integer userId){
        //记录当前用户
        onlineUserStatsService.online(userId);
        // 清除超过2分钟都没发送过心跳包的用户
        onlineUserStatsService.clear(Duration.ofMinutes(2));

        // 获取1分钟内，发送过心跳包的用户数量，也就是在线用户的数量
        Long count= onlineUserStatsService.count(Duration.ofMinutes(1));
        return Result.success(count);

    }

    //注销
    @ApiOperation(value="用户注销")
    @GetMapping("/logout")
    public Result<?> logout(HttpSession session){
        session.invalidate();
        return Result.success();
    }


    //全部
    @ApiOperation(value="获取全部用户")
    @GetMapping("/allUser")
    public Result<?> allUser() {
        Collection<QaqUser> res =  qaqUserDao.selectAllUser();
        return Result.success(res);
    }
}
